<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>admin管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

    <!-- 上传图片框样式，不使用上传组件可以删除此样式 -->
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 150px;
            height: 70px;
            line-height: 150px;
            text-align: center;
        }
        .avatar {
            width: 150px;
            height: 70px;
            display: block;
        }

    </style>
</head>
<body class="hold-transition">
<div id="app">

    <div class="content-header">
        <h1>管理<small>admin管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>管理</el-breadcrumb-item>
            <el-breadcrumb-item>admin管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <!--查询表单-->
                <el-form :inline="true" >
                          <el-form-item label="用户名">
<el-input v-model="searchMap.loginName" placeholder="用户名" class="filter-item" ></el-input></el-form-item>
          <el-form-item label="密码">
<el-input v-model="searchMap.password" placeholder="密码" class="filter-item" ></el-input></el-form-item>
          <el-form-item label="状态">
<el-input v-model="searchMap.status" placeholder="状态" class="filter-item" ></el-input></el-form-item>

                <el-button class="dalfBut" @click="fetchData()">查询</el-button>
                <el-button type="primary"  class="butT" @click="formVisible=true;pojo={};oldUserName='';ruleForm={};ruleForm.roleIds=[];">新增</el-button>
                </el-form>
            </div>

            <el-table :data="tableData" border style="width: 100%">
                          <el-table-column prop="id" label="id" width="80"></el-table-column>
          <el-table-column prop="loginName" label="用户名" width="80"></el-table-column>
          <!--<el-table-column prop="password" label="密码" width="80"></el-table-column>-->
          <el-table-column prop="status" label="状态" width="80"></el-table-column>

                    <el-table-column
                            label="操作"  >
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row.id);" size="mini" type="primary" size="small">修改</el-button>
                            <el-button @click="dele(scope.row.id)" size="mini" type="danger" size="small">删除</el-button>
                        </template>
                    </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @size-change="fetchData"
                        @current-change="fetchData"
                        :current-page.sync="currentPage"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="size"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
            <div class="add-form">
                <!--弹出窗口-->
                <el-dialog  title="编辑" :visible.sync="formVisible"  >
                    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm" style="margin: auto auto">
                        <el-form-item label="账号名:" prop="username">
                            <el-input v-model.number="ruleForm.username" style="width: auto" :disabled=flag></el-input>
                        </el-form-item>

                        <el-form-item label="角色:" prop="roleIds">
                            <template slot-scope="scope">
                                <el-select v-model="ruleForm.roleIds" multiple="true" filterable placeholder="请选择">
                                    <el-option
                                            v-for="item in options"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </template>
                        </el-form-item>
                        <el-form-item label="旧密码:" prop="oldPass">
                            <el-input type="password" v-model.string="ruleForm.oldPass" style="width: auto" placeholder="必须输入旧密码"></el-input>
                        </el-form-item>
                        <el-form-item label="新密码:" prop="pass">
                            <el-input type="password" v-model="ruleForm.pass" autocomplete="off" style="width: auto"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码:" prop="checkPass">
                            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" style="width: auto"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <!--<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>-->
                            <el-button type="primary" @click="save()">提交</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>
            </div>

        </div>
    </div>


</div>
</body>
<script src="/js/vue.js"></script>
<script src="/js/axios.js"></script>
<script src="/js/elementui.js"></script>
<script>
     new Vue({
        el: '#app',
        data(){
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            var validateUserName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入用户名'));
                } else {
                    axios.get(`/admin/checkRepeatName.do?loginName=`+this.ruleForm.username).then(response=>{
                        if(response.data.code==0){
                            callback();
                        }else{
                            if(this.oldUserName==value){ //如果名字和原来的名字一样，则通过
                                callback();
                            }else{
                                callback(new Error('该用户名重复，请重新选择'));
                            }
                        }
                    });
                }
            };
            var validateroleIds = (rule, value, callback) => {
                if (value.length === '') {
                    callback(new Error('请必须选择一个角色'));
                } else {

                    callback();
                }
            };
            var validateOldPass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入旧密码'));
                } else {

                    // if (this.ruleForm.checkPass !== '') {
                    //     this.$refs.ruleForm.validateField('checkPass');
                    // }

                    callback();
                }
            };
            return {
                tableData: [],
                currentPage: 1,
                total: 10,
                size: 10,
                searchMap: {},
                formVisible: false,
                imageUrl: '',
                isupdate: 0,
                oldUserName: '',//判断账号名是否应该显示，根据是否有pojo.id,默认为false，目前没用到
                options:[],//要选择的角色集合
                ruleForm: {
                    id:'',
                    username:'',
                    roleIds:[],
                    pass: '',
                    checkPass: '',
                    oldPass:''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    username: [
                        { validator: validateUserName, trigger: 'blur' }
                    ],
                    roleIds: [
                        { validator: validateroleIds, trigger: 'blur' }
                    ],
                    oldPass: [
                        { validator: validateOldPass, trigger: 'blur' }
                    ]
                }
            }
        },
        created(){
            this.fetchData();
        },
        methods:{
            fetchData (){
                axios.post(`/admin/findPage.do?page=${this.currentPage}&size=${this.size}`,this.searchMap).then(response => {
                    this.tableData = response.data.rows;
                    this.total = response.data.total;
                });
                axios.get(`/role/findAll.do`).then(response=>{
                    this.options=response.data;
                })
            },
            save (){


                axios.post(`/admin/${this.ruleForm.id==null?'add':'update'}.do`,this.ruleForm).then(response => {
                    if(response.data.code=='0'){
                        this.$alert('操作成功','信息提示');
                        this.ruleForm={};//清空表格
                        this.ruleForm.roleIds=[];//清空表格
                        this.formVisible = false ;//关闭窗口
                        this.oldUserName='';//旧名字设置为空
                        this.fetchData (); //刷新列表

                    }
                    if(response.data.code==2){//如果修改了当前账号的账号名
                        this.ruleForm={};//清空表格
                        this.ruleForm.roleIds=[];//清空表格
                        this.formVisible = false ;//关闭窗口
                        this.oldUserName='';//旧名字设置为空

                        alert(response.data.message);

                        //执行退出登录操作
                        this.exit();

                    }else {
                        this.$alert('操作失败:'+response.data.message,'错误提示');
                    }
                });

            },
            edit (id){
                this.formVisible = true; // 打开窗口

                this.oldUserName='';

                // this.flag='true'
                // 调用查询
                axios.get(`/admin/findById.do?id=${id}`).then(response => {
                    this.ruleForm.id=response.data.id;
                    this.ruleForm.username=response.data.username;
                    this.ruleForm.roleIds=response.data.roleIds;
                    this.ruleForm.oldPass=response.data.oldPass;
                    this.ruleForm.pass=response.data.pass;
                    this.ruleForm.checkPass=response.data.checkPass;

                    this.oldUserName=response.data.username;//获取旧名字

                    // this.imageUrl=this.pojo.image //显示图片  如页面有图片上传功能放开注释
                })
            },
            dele (id){
                this.$confirm('确定要删除此记录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then( () => {
                    axios.get(`/admin/delete.do?id=${id}`).then(response => {
                        if(response.data.code==0){
                            this.fetchData (); //刷新列表
                        }else{
                            this.$alert(response.data.message,'信息提示');
                        }

                    })
                })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post(`/admin/updatePassword.do`,this.ruleForm).then(response=>{
                            if(response.data.code=='0'){

                            this.$alert('修改密码成功','页面提示');
                            window.location.href="/login.html";//返回到登录界面

                        }else{

                            this.$alert('操作失败','错误提示');
                        }

                    });
                    } else {
                        console.log('error submit!!');
                return false;
            }
            });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            goBack(){
                window.location.href="/main.html";
            },
            exit(){
                axios.get(`/logout`).then(response=>{
                    // window.location.href='/main.html';
                    window.location.href='login.html';
                    // window.location.href='request.getContextPath()/resource/login';
                });
            }

        }
    })
</script>
</html>
